<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;

class User extends Controller
{
    public function _empty()
    {
        return '你访问到了不存在的方法';
    }


    public function index()
    {
        // 原生
//        $sql = 'SELECT * FROM user';
//        $list = Db::query($sql);

        $sql = 'SELECT * FROM user WHERE id = ?';
        $list = Db::query($sql, [4]);
        // DB
        $list = Db::table('user')->select();

        $list = Db::table('lover')->order(['id'=>'desc'])->select();

        // 助手函数
        $list = db('user')->field(['id','name'])->select();

        dump($list);
    }

    //  插入
    public function insert()
    {
        // 原生
//        $sql = 'INSERT INTO user (id,name,age) VALUES (null, "t1", 66)';
//        $row =  Db::execute($sql);

//        $sql = 'INSERT INTO user (name,age) VALUES (:n,:a)';
//        $row =  Db::execute($sql, ['n'=>'t2', 'a'=>'77']);

        // DB
        $data = ['name'=>'t5', 'age'=>'33'];
//        $row = Db::table('user')->insert($data);
//        $row = Db::name('user')->insertGetId($data);

        // 助手函数
        $row = db()->table('user')->insert($data);

        dump($row);
    }

    //  删除
    public function delete()
    {
        // 原生
//        $sql = 'DELETE FROM user WHERE name="t2"';
//        $row =  Db::execute($sql);

        // DB
//        $row = Db::table('user')->delete(52);

//        $row = Db::table('user')->where('name','t1')->delete();

        // 助手函数
        $row = db()->table('user')->delete(44);

        dump($row);
    }


    //  更新
    public function update()
    {
        // 原生
//        $sql = 'UPDATE user SET  age = "19" WHERE id = "1"';
//        $row =  Db::execute($sql);

        // DB
//        $row =  Db::table('user')->where('id',1)->update(['age'=>'88','sex'=>0]);

        // 助手函数
        $row =  db()->table('user')->where('id',1)->update(['age'=>'18','sex'=>1]);

        dump($row);
    }


}
